home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9207.ARJ / 1007040A < prev    next >
Text File  |  1992-06-03  |  10KB  |  291 lines

  1. /*                    Listing 1                     */
  2. /*****************************************************
  3.                 Name: SQL_PROC.C
  4.          Description: Library of functions to support
  5.                       stored SQL procedures with
  6.                       db_Vista and db_Query
  7. Global Function List: sqlproc_del
  8.                       sqlproc_exec
  9.                       sqlproc_fetch_dev
  10.                       sqlproc_fetch_str
  11.                       sqlproc_find
  12.                       sqlproc_mod
  13.                       sqlproc_retrv
  14.                       sqlproc_store
  15.          Portability: Standard C or MS Windows with
  16.                       db_Vista and db_Query Librarys.
  17. *****************************************************/
  18.  
  19. /* Standard C */
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24.  
  25. /* MS Windows */
  26. #if defined ( _WINDOWS )
  27. #include <windows.h>
  28. #endif
  29.  
  30. /* db_Vista and db_Query */
  31. #define ANSI 1
  32. #define LINT_ARGS 1
  33. #include <vista.h>
  34. #include <prto_fix.h>
  35. #include <query.h>
  36.  
  37. /* Own */
  38. #include <sql_proc.h>
  39. #include <sqlpproc.h>
  40.  
  41. /*****************************************************
  42.        Name: sqlproc_fetch_dev
  43.  Parameters: Dev - pointer to output device
  44.      Return: Number of output lines
  45. Description: After an sql statement is executed, this
  46.              function will fetch data to the device
  47.              specified.
  48. *****************************************************/
  49. int sqlproc_fetch_dev( FILE *Dev )
  50.    {
  51.    char *Str;
  52.    int NumLines = 0;
  53.    while (( Str = sqlproc_fetch_str()) != NULL )
  54.       {
  55.       fprintf( Dev, "%s\n", Str );
  56.       free( Str );
  57.       NumLines++;
  58.       }
  59.    return ( NumLines );
  60.    }   /* function sqlproc_fetch_dev */
  61.  
  62. /*****************************************************
  63.        Name: sqlproc_fetch_str
  64.      Return: pointer to string on heap
  65. Description: After an sql statement is executed, this
  66.              function will fetch data to a dynamically
  67.              allocated string on the heap.  Columns
  68.              are comma and tab delimited.
  69. *****************************************************/
  70. char *sqlproc_fetch_str( void )
  71.    {
  72.    VALUE Vals[SQL_MAX_COLUMNS];
  73.    int Count, i;
  74.    size_t Size = 0;
  75.    char StrBuf[BUFSIZ];
  76.    char *StrPntr, *TmpPntr;
  77.    if ( q_nextval( &Count, Vals ) != Q_OKAY )
  78.       return ( NULL );
  79.    if (( StrPntr = malloc( 1 )) == NULL )
  80.       return ( NULL );
  81.    *StrPntr = '\0';
  82.    for ( i = 0; i < Count; i++ )
  83.       {
  84.       switch ( Vals[i].type )
  85.          {
  86.          default:
  87.          case '\0':
  88.             break;
  89.          case 'l':
  90.             sprintf( StrBuf, "%ld",
  91.                   Vals[i].result.lv );
  92.             break;
  93.          case 'i':
  94.             sprintf( StrBuf, "%d",
  95.                   Vals[i].result.iv );
  96.             break;
  97.          case 'F':
  98.             sprintf( StrBuf, "%g",
  99.                   Vals[i].result.dv );
  100.             break;
  101.          case 'c':
  102.             #if defined( _WINDOWS )
  103.             sprintf( StrBuf, "%s",
  104.                   Vals[i].result.cv.ptr );
  105.             #else
  106.             sprintf( StrBuf, "%s",
  107.                   Vals[i].result.cv );
  108.             #endif
  109.             break;
  110.          }   /* switch Vals */
  111.       Size += strlen( StrBuf ) + 1;
  112.       if ( i > 0 ) Size++;
  113.       TmpPntr = StrPntr;
  114.       if (( StrPntr = realloc( StrPntr, Size ))
  115.             == NULL )
  116.          {
  117.          free( TmpPntr );
  118.          return ( NULL );
  119.          }
  120.       if ( i > 0 ) strcat( StrPntr, ",\t" );
  121.       strcat( StrPntr, StrBuf );
  122.       }   /* for i */
  123.    return ( StrPntr );
  124.    }   /* function sqlproc_fetch_str */
  125.  
  126. /*****************************************************
  127.        Name: sqlproc_store
  128.  Parameters: Name - name of SQL stored procedure
  129.              Description - description of SQL command
  130.              Command - SQL command (text)
  131.      Return: result of d_fillnew()
  132. Description: Stores the values passed as parameters in
  133.              a record of type SQL_PROC in the database
  134. *****************************************************/
  135. int sqlproc_store( char *Name, char *Description,
  136.       char *Command )
  137.    {
  138.    struct sql_proc SqlProc;
  139.    strcpy( SqlProc.sql_name, Name );
  140.    strcpy( SqlProc.sql_description, Description );
  141.    strcpy( SqlProc.sql_command, Command );
  142.    return ( d_fillnew( SQL_PROC, &SqlProc, CURR_DB ));
  143.    }   /* function sqlproc_store */
  144.  
  145. /*****************************************************
  146.        Name: sqlproc_mod
  147.  Parameters: Name - name of SQL stored procedure
  148.              Description - description of SQL command
  149.              Command - SQL command (text)
  150.      Return: result of sqlproc_find() or d_crwrite()
  151. Description: Modifies the current record or record
  152.              associated with the key Name with the
  153.              new values passed as parameters.
  154. *****************************************************/
  155. int sqlproc_mod( char *Name, char *Description,
  156.       char *Command )
  157.    {
  158.    int Status;
  159.    Status = sqlproc_find( Name );
  160.    if ( Status == TRUE || Status == S_OKAY )
  161.       {
  162.       if ( Description != NULL )
  163.          Status = d_crwrite( SQL_DESCRIPTION,
  164.                Description );
  165.       if ( Command != NULL )
  166.          Status = d_crwrite( SQL_COMMAND,
  167.                Command );
  168.       }
  169.    return ( Status );
  170.    }   /* function sqlproc_mod */
  171.  
  172. /*****************************************************
  173.        Name: sqlproc_retrv
  174.  Parameters: Name - name of SQL stored procedure
  175.              SqlProc - pointer to sql_proc struct to
  176.              load with data
  177.      Return: result of sqlproc_find() or d_recread()
  178. Description: Retrives the data in a record of type
  179.              SQL_PROC.  Either the current record
  180.              (if Name == NULL ) or the record
  181.              associated with the key Name is retrived.
  182. *****************************************************/
  183. int sqlproc_retrv( char *Name,
  184.       struct sql_proc *SqlProc )
  185.    {
  186.    int Status;
  187.    Status = sqlproc_find( Name );
  188.    if ( Status == TRUE || Status == S_OKAY )
  189.       Status = d_recread( SqlProc, CURR_DB );
  190.    return ( Status );
  191.    }   /* function sqlproc_retrv */
  192.  
  193. /*****************************************************
  194.        Name: sqlproc_exec
  195.  Parameters: Name - name of SQL stored procedure
  196.      Return: result of sqlproc_retrv() or q_sqlinit()
  197.              or S_NOMEMORY if out of heap space.
  198. Description: A SQL stored procedure is retrived from
  199.              the database, parsed and executed.
  200. *****************************************************/
  201. int sqlproc_exec( char *Name, ... )
  202.    {
  203.    va_list VarArgList;
  204.    int ErrPos, Status;
  205.    struct sql_proc SqlProc;
  206.    #if defined( _WINDOWS )
  207.    char ErrMsg[MAXERRMSGTXT + 1];
  208.    #else
  209.    char *ErrMsg = NULL;
  210.    #endif
  211.    Status = sqlproc_retrv( Name, &SqlProc );
  212.    if ( Status != S_OKAY ) return ( Status );
  213.    SqlCommand = malloc( SQL_COMMAND_LEN + 1 );
  214.    if ( SqlCommand == NULL ) return ( S_NOMEMORY );
  215.    va_start( VarArgList, Name );
  216.    vsprintf( SqlCommand, SqlProc.sql_command,
  217.          VarArgList );
  218.    #if defined( _WINDOWS )
  219.    Status = q_sqlinit( SqlCommand, &ErrPos, ErrMsg );
  220.    #else
  221.    Status = q_sqlinit( SqlCommand, &ErrPos, &ErrMsg );
  222.    #endif
  223.    va_end( VarArgList );
  224.    #if defined( _DEBUG )
  225.    if ( Status > 0 )
  226.       {
  227.       /* Status != Q_OKAY, Q_END, Q_SET, Q_DEFINE,
  228.       ** Q_FIELD, Q_VIEW, Q_BATCH, Q_HELP, Q_SHOW */
  229.       char *ErrMsgOut;
  230.       if (( ErrMsgOut = malloc( SQL_COMMAND_LEN +
  231.             MAXERMESTXT )) != NULL )
  232.          {
  233.          sprintf( ErrMsgOut,
  234.                "%s\n%s at position %d.",
  235.                SqlCommand, ErrMsg, ErrPos );
  236.          #if defined( _WINDOWS )
  237.          MessageBox( NULL, ErrMsgOut,
  238.             "SQL Parse Error", MB_OK );
  239.          #else
  240.          fprintf( stderr, "%s\n", ErrMsgOut );
  241.          #endif
  242.          free( ErrMsgOut );
  243.          }
  244.       }
  245.    #endif
  246.    free( SqlCommand );
  247.    return ( Status );
  248.    }   /* function sqlproc_exec */
  249.  
  250. /*****************************************************
  251.        Name: sqlproc_del
  252.  Parameters: Name - name of SQL stored procedure
  253.      Return: result of sqlproc_find() or d_delete()
  254. Description: The record associated with the key Name
  255.              is deleted or the current record (if
  256.              Name == NULL and current record is right
  257.              type) is deleted.
  258. *****************************************************/
  259. int sqlproc_del( char *Name )
  260.    {
  261.    int Status;
  262.    Status = sqlproc_find( Name );
  263.    if ( Status == TRUE || Status == S_OKAY )
  264.       Status = d_delete( CURR_DB );
  265.    return ( Status );
  266.    }   /* function sqlproc_del */
  267.  
  268. /*****************************************************
  269.        Name: sqlproc_find
  270.  Parameters: Name - name of SQL stored procedure
  271.      Return: result of d_keyfind(), FAIL or TRUE
  272. Description: The SQL_PROC record associated with the
  273.              key Name is set as the current record.
  274.              If Name is NULL the current record is
  275.              checked to see if it is type SQL_PROC.
  276. *****************************************************/
  277. int sqlproc_find( char *Name )
  278.    {
  279.    if ( Name == NULL )
  280.       {
  281.       int RecType;
  282.       if ( d_crtype( &RecType, CURR_DB ) != S_OKAY
  283.             || RecType != SQL_PROC )
  284.          return ( FAIL );
  285.       else return ( TRUE );
  286.       }
  287.    return ( d_keyfind( SQL_NAME, Name, CURR_DB ));
  288.    }   /* function sqlproc_find */
  289.  
  290. /* End of File */
  291.